home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / sprtxtrn.sea / Support Tools eXternals 1.2.5 / card_38514.txt < prev    next >
Text File  |  1990-11-13  |  8KB  |  252 lines

  1. -- card: 38514 from stack: in.5
  2. -- bmap block id: 43542
  3. -- flags: 0000
  4. -- background id: 3858
  5. -- name: NumToBinary
  6. ----- HyperTalk script -----
  7. on hideObjects
  8.   hide cd fld "label"
  9.   hide cd fld "label 2"
  10.   hide cd fld "decimal"
  11.   hide cd fld "binary"
  12.   hide cd btn "convert"
  13. end hideObjects
  14.  
  15. on showObjects
  16.   show cd fld "label"
  17.   show cd fld "label 2"
  18.   show cd fld "decimal"
  19.   show cd fld "binary"
  20.   show cd btn "convert"
  21. end showObjects
  22.  
  23.  
  24. -- part 1 (button)
  25. -- low flags: 00
  26. -- high flags: A002
  27. -- rect: left=82 top=191 right=225 bottom=175
  28. -- title width / last selected line: 0
  29. -- icon id / first selected line: 0 / 0
  30. -- text alignment: 1
  31. -- font id: 0
  32. -- text size: 12
  33. -- style flags: 8192
  34. -- line height: 16
  35. -- part name: Convert
  36. ----- HyperTalk script -----
  37. on mouseUp
  38.   put NumToBinary(cd fld "decimal") into cd fld "binary"
  39. end mouseUp
  40.  
  41.  
  42.  
  43.  
  44. -- part 2 (field)
  45. -- low flags: 00
  46. -- high flags: 0002
  47. -- rect: left=103 top=159 right=179 bottom=240
  48. -- title width / last selected line: 0
  49. -- icon id / first selected line: 0 / 0
  50. -- text alignment: 0
  51. -- font id: 3
  52. -- text size: 12
  53. -- style flags: 256
  54. -- line height: 16
  55. -- part name: Decimal
  56. ----- HyperTalk script -----
  57. on ReturnInField
  58.   send mouseUp to cd btn "convert"
  59. end ReturnInField
  60.  
  61. on EnterInField
  62.   send mouseUp to cd btn "convert"
  63. end EnterInField
  64.  
  65.  
  66. -- part 3 (field)
  67. -- low flags: 00
  68. -- high flags: 0002
  69. -- rect: left=103 top=234 right=254 bottom=240
  70. -- title width / last selected line: 0
  71. -- icon id / first selected line: 0 / 0
  72. -- text alignment: 0
  73. -- font id: 3
  74. -- text size: 12
  75. -- style flags: 256
  76. -- line height: 16
  77. -- part name: Binary
  78.  
  79.  
  80. -- part 4 (field)
  81. -- low flags: 00
  82. -- high flags: 0000
  83. -- rect: left=45 top=161 right=178 bottom=96
  84. -- title width / last selected line: 0
  85. -- icon id / first selected line: 0 / 0
  86. -- text alignment: 65535
  87. -- font id: 3
  88. -- text size: 10
  89. -- style flags: 0
  90. -- line height: 13
  91. -- part name: label
  92.  
  93.  
  94. -- part 5 (field)
  95. -- low flags: 00
  96. -- high flags: 0000
  97. -- rect: left=45 top=237 right=254 bottom=96
  98. -- title width / last selected line: 0
  99. -- icon id / first selected line: 0 / 0
  100. -- text alignment: 65535
  101. -- font id: 3
  102. -- text size: 10
  103. -- style flags: 0
  104. -- line height: 13
  105. -- part name: label 2
  106.  
  107.  
  108. -- part contents for background part 38
  109. ----- text -----
  110. 32/50
  111.  
  112. -- part contents for card part 4
  113. ----- text -----
  114. Decimal
  115.  
  116. -- part contents for card part 5
  117. ----- text -----
  118. Binary
  119.  
  120. -- part contents for card part 2
  121. ----- text -----
  122. 253
  123.  
  124. -- part contents for card part 3
  125. ----- text -----
  126. 11111101
  127.  
  128. -- part contents for background part 20
  129. ----- text -----
  130.      This XFCN returns the binary representation (base 2, all 1's and 0's) of a decimal number.
  131.  
  132.      Calling syntax : NumToBinary(decimalNumber)
  133.   DECIMALNUMBER: the number to convert.  
  134.  
  135.  
  136. -- part contents for background part 42
  137. ----- text -----
  138. {NumToBinary(theNumber)}
  139. { Change a number to a string representing it's binary value. }
  140. {}
  141. {   brought to you by:      Anup Murarka             Eric Carlson         }
  142. {                       ALINK:  SKEPTIC           ALINK:  cyNic   }
  143. {                                   CIS:  76004,3356         }
  144. {}
  145. {               We are part of the Support Tools Development Group,     }
  146. {               Apple Computer, Inc.      }
  147. {}
  148. {               please DO NOT contack Mac DTS for support of this code!    }
  149. {}
  150. {               please DO contact the authors for support of this code!     }
  151. {}
  152. {               Send comments, bug reports, requests to any of the above   }
  153. {               E-mail addresses or to:}
  154. {}
  155. {                           (one of us)                  }
  156. {                           Apple Computer, Inc.          }
  157. {                           900 E. Hamilton, Ave.          }
  158. {                           Campbell, CA   95008      }
  159. {                           M/S 72-L                     }
  160. {}
  161. {   Copyright:   ┬⌐ 1989, 1990 by Apple Computer, Inc., all rights reserved.     }
  162. {}
  163. { written by Eric Carlson                                        }
  164. { AppleLink:  cyNic                                              }
  165. { modification history                                                                                        }
  166. {          Date              Initials                                    Comments                               }
  167. {          ----              ------      --------------------------------------------------}
  168. {       11/20/89         ec            first written                                                               }
  169. {       6/6/90             ec            commented, cleaned code                                                 }
  170. {}
  171. unit NumToBin;
  172.  
  173. interface
  174.     uses
  175.         HyperXCMD;
  176.  
  177.     procedure main (paramPtr: XCmdPtr);
  178.  
  179. implementation
  180.  
  181.     function askedForHelp (paramPtr: XCmdPtr;
  182.                                     syntaxMsg: Str255;
  183.                                     copyRightMsg: Str255): boolean;
  184. {}
  185. {   check to see if the user sent a '?' or a '!' as }
  186. { the only parameter. if so we will respond with }
  187. { the calling syntax or the copyright/version info }
  188. { for this external }
  189. {}
  190.         var
  191.             firstStr: str255;
  192.     begin
  193.         askedForHelp := false;
  194.         if paramPtr^.paramCount = 1 then
  195.             begin
  196.                 ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr);
  197.                     { what is the first param? }
  198.                 if firstStr = '?' then
  199.                     begin
  200.                         paramPtr^.returnValue := PasToZero(paramPtr, syntaxMsg);
  201.                         askedForHelp := true
  202.                     end  { asked for help }
  203.                 else if firstStr = '!' then
  204.                     begin
  205.                         paramPtr^.returnValue := PasToZero(paramPtr, copyRightMsg);
  206.                         askedForHelp := true
  207.                     end;     { asked for copyright info }
  208.             end;     { one parameter passed }
  209.     end;     { function }
  210.  
  211.     function NumberToString (paramPtr: XCmdPtr;
  212.                                     num: LONGINT): Str255;
  213. { use the toolbox call rather than HC's }
  214.         var
  215.             tempStr: str255;
  216.     begin
  217.         NumToString(num, tempStr);
  218.         NumberToString := tempStr;
  219.     end;
  220.  
  221.     procedure NumToBin (paramPtr: XCMDPtr);
  222.         var
  223.             copyRtStr, syntaxStr: str255;
  224.             binaryNum: str255;
  225.             theNumber: LONGINT;
  226.             bitCount, bits: integer;
  227.             binArray: string[2];
  228.     begin
  229.         syntaxStr := 'NumToBinary(theNumber)';
  230.         copyRtStr := '┬⌐ 1989,1990 Apple Computer, Inc. v.1.0,  by Eric Carlson.';
  231.         if askedForHelp(paramPtr, syntaxStr, copyRtStr) then                    { asking for help? }
  232.             exit(NumToBin);
  233.         ZeroToPas(paramPtr, paramPtr^.params[1]^, binaryNum);                 { the value to convert }
  234.         theNumber := StrToNum(paramPtr, binaryNum);
  235.         binArray := '01';                                                               { we'll return some combination of these }
  236.         bitCount := round(ln(theNumber) / ln(2));                                     { the number of bits in our number }
  237.         if (bitCount mod 8 <> 0) or (bitCount = 0) then                              { does our number have an odd number of bits? }
  238.             bitCount := (bitCount div 8 + 1) * 8;                                       { make it an even multiple of 8 }
  239.         binaryNum := binArray[BAND(theNumber, $01) + 1];                         { grab the first bit }
  240.         for bits := 1 to bitCount - 1 do                                               { and step through the rest }
  241.             begin
  242.                 theNumber := BSR(theNumber, 1);                                             { shift right, drop the bit we just examined }
  243.                 binaryNum := concat(binArray[BAND(theNumber, $01) + 1], binaryNum);
  244.             end;
  245.         paramPtr^.returnValue := PasToZero(paramPtr, binaryNum);
  246.     end;
  247.  
  248.     procedure main (paramPtr: XCmdPtr);
  249.     begin
  250.         NumToBin(paramPtr);
  251.     end;
  252. end.